package cn.xinzhi.main;

import cn.xinzhi.dao.IUserInfoDao;
import cn.xinzhi.entity.UserInfo;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.List;

public class Mybatis_Main {
    public static void main(String[] args) {
        SqlSession ss = null;
        try {
            InputStream ii = Resources.getResourceAsStream("mybatis-config.xml");
            SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(ii);
            ss = ssf.openSession();
            IUserInfoDao iu = ss.getMapper(IUserInfoDao.class);

//            InputStream ii = Resources.getResourceAsStream("mybatis-config.xml");
//            SqlSessionFactory ssf = new SqlSessionFactoryBuilder().build(ii);
//            ss = ssf.openSession();
//            IUserInfoDao iu = ss.getMapper(IUserInfoDao.class);

            //------------查全部---------------
            List<UserInfo> iu2 = iu.getAll();
            for (UserInfo ui:iu2) {
                System.out.println(ui.getUsername() + "---" + ui.getPassword() + "---" + ui.getUserage());
//            List<UserInfo> iu2 = iu.getAll();
//            for (UserInfo ui:iu2) {
//                System.out.println(ui.getUsername() + "---" + ui.getPassword() + "---" + ui.getUserage());

            }

            //------------ID查用户名---------------
            UserInfo ui = iu.getInfoById(3);
            System.out.println(ui.getUsername());
//            UserInfo ui = iu.getInfoById(3);
//            System.out.println(ui.getUsername());

            //------------用户名密码查年龄---------------
            UserInfo uiName = iu.getUserByName("张三","111");
            System.out.println(uiName.getUserage());
//            UserInfo uiName = iu.getUserByName("张三","111");
//            System.out.println(uiName.getUserage());

            //------------查询总记录---------------
            int count = iu.getCount();
            System.out.println(count);
//            int count = iu.getCount();
//            System.out.println(count);

            //------------新增用户---------------
            UserInfo uii = new UserInfo();
            uii.setId(7);
            uii.setUsername("ww三");
            uii.setPassword("1123");
            uii.setUserage("881");
            int a1 = iu.addUser(uii,1);
            System.out.println(a1 > 0 ? "成功":"失败");
            ss.commit();
//            UserInfo uii = new UserInfo();
//            uii.setId(7);
//            uii.setUsername("ww三");
//            uii.setPassword("1123");
//            uii.setUserage("881");
//            int a1 = iu.addUser(uii,1);
//            System.out.println(a1 > 0 ? "成功":"失败");
//            ss.commit();
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            if(ss!=null){
                ss.close();
            }
        }
    }
}
